<?php

namespace Project\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\Entity\User;
use Application\Model\Entity\Developer;

class IndexController extends AbstractActionController
{
    protected $_objectManager;
    
    public function indexAction()
    {   
        $users = $this->getObjectManager()->getRepository('\Application\Model\Entity\User')->findAll();
        $projects = $this->getObjectManager()->getRepository('\Application\Model\Entity\Project')->findAll();
        
        return new ViewModel(array(
            'users'     => $users,
            'projects'  => $projects
                    )
                );
    }
    
    public function showAction()
    { 
        $id = $this->params()->fromRoute('id');
        if(isset($id))
        {          
            
            $project = $this->getObjectManager()->find('\Application\Model\Entity\Project', $id);
                       
            if(isset($project) && !empty($project))
            {
            return new ViewModel(array(
                        'project' => $project,
                    ));
            }
        }        
        
        return new ViewModel();
    }
    
    public function allAction() {
        $projects = $this->params()->fromRoute('projects');
        if (isset($projects) && !empty($projects)) 
        {
            return new ViewModel(array(
                'projects' => $projects
            ));
        }
        else 
        {
            $projects = $this->getObjectManager()->getRepository('\Application\Model\Entity\Project')->findAll();
            
            return new ViewModel(array(
                'projects' => $projects
            ));
        }
    }

    public function userProjectsAction()
    {        
        $id = $this->params()->fromRoute('id');
        if(isset($id))
        {          
            $projects = $this->getObjectManager()->getRepository('\Application\Model\Entity\Project')->findBy(array('id' => $id));            
           
            if(isset($projects) && !empty($projects))
            {                
                 return new ViewModel(array(
                  'projects' => $projects
                  ));
            }
        }        
        else 
        {
            return $this->redirect()->toRoute('home');
        }   
    }
    
    public function addProjectAction()
    {
        if ($this->request->isPost()) {
            $project = new \Application\Model\Entity\Project();
            $project->setTitle($this->getRequest()->getPost('title'));
            $project->setDescription($this->getRequest()->getPost('description'));
            $project->setDescription(1);
            $project->setShortdescription($this->getRequest()->getPost('shortDescription'));
            $user = $this->getObjectManager()->find('\Application\Model\Entity\User', 1);
            $project->setUsers($user);
            
            $category = $this->getObjectManager()->find('\Application\Model\Entity\Category', 1);
            $project->setCategory($category);
            
            $status = $this->getObjectManager()->find('\Application\Model\Entity\Status', 1);
            $project->setStatuses($status);            
            
            $this->getObjectManager()->persist($project);
            $this->getObjectManager()->flush();
            $newId = $project->getId();

            return $this->redirect()->toRoute('user', array('controller' => 'home', 'action' => 'show'));
        }
        
        return new ViewModel();
    }
    
    
    
    public function loginAction()
    {
        if ($this->request->isPost()) {
            
            $password = $this->getRequest()->getPost('password');
            $email = $this->getRequest()->getPost('email');
             
            $user = $this->getObjectManager()->getRepository('\Application\Model\Entity\User')->findBy(array('email' => $email, 'password' => $password));

           
             if(empty($user)){
                 return new ViewModel(array('resp' => 'Wprowadzony niprawidłowy login lub hasło'));                
             }
             else{
                 return $this->redirect()->toRoute('user', array('controller' => 'home', 'action' => 'show'));    
             }
        }
        
        return new ViewModel();
        
    }
    

    
    public function addAction()
    {
        if ($this->request->isPost()) {
            $user = new User();
            $user->setFirstname($this->getRequest()->getPost('firstname'));
            $user->setLastname($this->getRequest()->getPost('lastname'));


            $this->getObjectManager()->persist($user);
            $this->getObjectManager()->flush();
            $newId = $user->getId();

            return $this->redirect()->toRoute('user', array('controller' => 'index', 'action' => 'index'));
        }
        
        return new ViewModel();
    }
    
    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        $user = $this->getObjectManager()->find('\Application\Model\Entity\User', $id);
        
        if ($this->request->isPost()) {
            $user->setFirstName($this->getRequest()->getPost('firstname'));
            $user->setLastName($this->getRequest()->getPost('lastname'));           
            $this->getObjectManager()->persist($user);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('user', array('controller' => 'index', 'action' => 'index'));
        }

        return new ViewModel(array('user' => $user));
    }
    
    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        $user = $this->getObjectManager()->find('\Application\Model\Entity\User', $id);

        if ($this->request->isPost()) {
            $this->getObjectManager()->remove($user);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('user', array('controller' => 'index', 'action' => 'index'));
        }

        return new ViewModel(array('user' => $user));
    }
    
    protected function getObjectManager()
    {
        if (!$this->_objectManager) {
            $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->_objectManager;
    }
}
